// pages/baseInfo/index.js
let app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    nickname:"",
    avatar:"",
    sex:"",
    isAuth:false,
    showAuth:true,
    eyeArr:[
      0.1,0.12,0.15,0.2,0.3,0.4,0.5,0.6,0.8,1.0,1.2,1.5
    ],
    both_vision:5,
    right_vision:5,
    left_vision:5,
    right_degrees:5,
    left_degrees:5,
    both_correct_vision:5,
    userinfo:{}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getUserInfo();
  },
  getUserInfo(){
    app.post('user/userInfo').then(res=>{
      console.log('000')
      console.log(res)
      console.log(res.data)
      let arr = ['both_vision','right_vision','left_vision','both_correct_vision']
        let eyes = this.data.eyeArr;
        for(let i = 0;i<arr.length;i++){
          let name = arr[i];
          let value = Number(res.data[name])
          for(let j = 0;j<eyes.length;j++){
            if(value == eyes[j]){
              this.setData({
                [name]:j
              })
            }
          }
        }
      let r = res.data
      if(r.nickname){
        console.log(111)
        this.setData({
          avatar:r.head_image,
          nickname:r.nickname,
          sex:r.sex,
          isAuth:true,
          showAuth:false,
          userinfo:r
        })
      }
    })
 },
  bindPickerChange(e){
    let name = e.target.dataset.name;
    console.log(name)
    this.setData({
      [name]:Number(e.detail.value)
    })
  },
  submitForm(e){
    if(!this.data.isAuth){
      this.setData({
        showAuth:true
      })
      return
    }
    let formData = e.detail.value;
    console.log(formData);
    for(let k in formData){
      if(!formData[k]){
        wx.showToast({
          title: '请继续完善信息',
          icon:'none'
        })
        return
      }
    }
    let data = this.data;
    let eyes = data.eyeArr;
    formData.both_vision = eyes[data.both_vision];
    formData.right_vision = eyes[data.right_vision];
    formData.left_vision =  eyes[data.left_vision];
    formData.both_correct_vision =  eyes[data.both_correct_vision];
    formData.head_image = this.data.avatar;
    formData.nickname = this.data.nickname;
    formData.sex = this.data.sex;
    app.post('user/saveInfo',formData).then(res=>{
      console.log(res)
      if(res.code == 1){
        wx.showToast({
          title: '提交成功',
          icon:'none'
        })
        wx.reLaunch({
          url: '/pages/index/index',
        })
      }
    })
  },
  cancle(){
    this.setData({
      showAuth:false
    })
  },
  getUserinfo(e){
      console.log(e)
      let data = e.detail.userInfo;
      this.setData({
        avatar:data.avatarUrl,
        nickname:data.nickName,
        sex:data.gender,
        isAuth:true,
        showAuth:false
      })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})